package com.happy_lab_management.demo.teacher.equipmentRepair.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.happy_lab_management.context.BaseContext;
import com.happy_lab_management.demo.admin.user.service.TeacherService;
import com.happy_lab_management.demo.technician.lab.service.LabService;
import com.happy_lab_management.entity.Lab;
import com.happy_lab_management.entity.LabSchedule;
import com.happy_lab_management.entity.Teacher;
import com.happy_lab_management.entity.VO.EquipmentRepairVO;
import com.happy_lab_management.result.Result;
import com.happy_lab_management.demo.teacher.equipmentRepair.service.EquipmentRepairService;
import com.happy_lab_management.entity.EquipmentRepair;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController("teacherEquipRepair")
@RequestMapping("/teacher/equipRepair")
@Slf4j
public class EquipmentRepairController {
    @Autowired
    private EquipmentRepairService equipmentRepairService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private LabService labService;

    /**
     * 列出当前教师用户所有报修申请
     * @return
     */
    @RequestMapping("/list")
    public Result<IPage<EquipmentRepairVO>> list(@RequestParam(required = false) String labNumber,
                                                 @RequestParam(required = false) String status,
                                                 @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Integer userId = BaseContext.getCurrentId();
        Teacher teacher = teacherService.selectByUserId(userId);
        QueryWrapper<EquipmentRepair> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacher_id", teacher.getId());
        if (labNumber != null && !labNumber.isEmpty()) {
            List<Integer> labIds = labService.list(new QueryWrapper<Lab>().like("lab_number", labNumber))
                    .stream().map(Lab::getId).collect(Collectors.toList());
            queryWrapper.in("lab_id", labIds);
        }
        if (status != null && !status.isEmpty()) {
            queryWrapper.eq("status", status);
        }
        Page<EquipmentRepair> page = new Page<>(pageNo, pageSize);
        IPage<EquipmentRepair> equipmentRepairPage = equipmentRepairService.page(page, queryWrapper);
        List<EquipmentRepairVO> equipmentRepairVOList = new ArrayList<>();
        for (EquipmentRepair equipmentRepair : equipmentRepairPage.getRecords()) {
            EquipmentRepairVO vo = new EquipmentRepairVO();
            vo.setId(equipmentRepair.getId());
            vo.setCompletionDescription(equipmentRepair.getCompletionDescription());
            vo.setRepairDate(equipmentRepair.getRepairDate());
            vo.setLabNumber(labService.getById(equipmentRepair.getLabId()).getLabNumber());
            vo.setApplicationDescription(equipmentRepair.getApplicationDescription());
            vo.setTeacherName(teacher.getName());
            vo.setStatus(equipmentRepair.getStatus());
            equipmentRepairVOList.add(vo);
        }
        IPage<EquipmentRepairVO> resultPage = new Page<>();
        resultPage.setRecords(equipmentRepairVOList);
        resultPage.setCurrent(equipmentRepairPage.getCurrent());
        resultPage.setSize(equipmentRepairPage.getSize());
        resultPage.setTotal(equipmentRepairPage.getTotal());
        return Result.success(resultPage);

    }
/*报修的内容包括：报修教师、实验室编号、故障描述(文本)、报修日
期*/

    /**
     * 添加维修申请
     * @param
     * @return
     */
    @RequestMapping("/add")
    public Result<String> add(@RequestParam Integer labNumber,@RequestParam String applicationDescription){
        EquipmentRepair equipmentRepair=new EquipmentRepair();
        Lab lab= labService.selectByLabNumber(labNumber);
        equipmentRepair.setLabId(lab.getId());
        Integer userId = BaseContext.getCurrentId();
        Teacher teacher = teacherService.selectByUserId(userId);
        equipmentRepair.setTeacherId(teacher.getId());
        // 设置当前时间为维修日期
        equipmentRepair.setRepairDate(new java.sql.Date(System.currentTimeMillis()));
        equipmentRepair.setStatus("未维修");
        equipmentRepair.setApplicationDescription(applicationDescription);
        equipmentRepairService.save(equipmentRepair);
        return Result.success("添加成功");
    }

}
